<%@ page import="java.sql.*" %>
<%@ page import="com.example.asa.DatabaseUtil" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>历史灾情信息</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Font Awesome 图标 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3f37c9;
            --light-primary: rgba(67, 97, 238, 0.1);
            --success-color: #4cc9f0;
            --warning-color: #f8961e;
            --error-color: #f72585;
            --light-gray: #f1f3f5;
            --mid-gray: #e9ecef;
            --dark-gray: #495057;
            --text-color: #212529;
            --border-radius: 12px;
            --card-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', system-ui, -apple-system, sans-serif;
            background-color: #f8f9fa;
            color: var(--text-color);
            line-height: 1.6;
            padding: 20px;
            min-height: 100vh;
        }

        .page-container {
            max-width: 1400px;
            margin: 0 auto;
        }

        /* 顶部导航栏 */
        .navbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: white;
            padding: 15px 25px;
            border-radius: var(--border-radius);
            box-shadow: var(--card-shadow);
            margin-bottom: 30px;
        }

        .navbar-title {
            display: flex;
            align-items: center;
            gap: 10px;
            color: var(--primary-color);
            font-size: 1.4rem;
            font-weight: 600;
        }

        .navbar-title i {
            font-size: 1.6rem;
        }

        .navbar-actions {
            display: flex;
            gap: 15px;
        }

        .btn {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 8px 18px;
            border-radius: 6px;
            font-weight: 500;
            font-size: 0.95rem;
            cursor: pointer;
            transition: all 0.3s ease;
            border: none;
        }

        .btn-primary {
            background-color: var(--primary-color);
            color: white;
        }

        .btn-primary:hover {
            background-color: var(--secondary-color);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(67, 97, 238, 0.2);
        }

        .btn-outline {
            background-color: transparent;
            color: var(--primary-color);
            border: 1px solid var(--primary-color);
        }

        .btn-outline:hover {
            background-color: var(--light-primary);
        }

        /* 页面标题区 */
        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
        }

        .page-title {
            font-size: 1.8rem;
            font-weight: 600;
            color: var(--text-color);
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .page-title i {
            color: var(--primary-color);
        }

        /* 统计卡片 */
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background-color: white;
            padding: 20px;
            border-radius: var(--border-radius);
            box-shadow: var(--card-shadow);
            transition: transform 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
        }

        .stat-title {
            color: var(--dark-gray);
            font-size: 0.9rem;
            margin-bottom: 8px;
        }

        .stat-value {
            font-size: 1.8rem;
            font-weight: 700;
            color: var(--primary-color);
            margin-bottom: 5px;
        }

        .stat-trend {
            display: flex;
            align-items: center;
            gap: 5px;
            font-size: 0.85rem;
        }

        .trend-up {
            color: var(--success-color);
        }

        .trend-down {
            color: var(--error-color);
        }

        /* 主内容区 */
        .content-card {
            background-color: white;
            border-radius: var(--border-radius);
            box-shadow: var(--card-shadow);
            overflow: hidden;
        }

        .card-header {
            padding: 20px 25px;
            border-bottom: 1px solid var(--mid-gray);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .card-title {
            font-size: 1.3rem;
            font-weight: 600;
            color: var(--text-color);
        }

        .card-actions {
            display: flex;
            gap: 10px;
        }

        /* 表格样式 */
        .table-wrapper {
            overflow-x: auto;
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
            min-width: 1000px;
        }

        .data-table th {
            background-color: var(--light-gray);
            color: var(--dark-gray);
            font-weight: 600;
            text-align: left;
            padding: 15px 20px;
            position: sticky;
            top: 0;
            z-index: 10;
        }

        .data-table td {
            padding: 15px 20px;
            border-bottom: 1px solid var(--mid-gray);
        }

        .data-table tr:hover {
            background-color: var(--light-primary);
        }

        .data-table tr:last-child td {
            border-bottom: none;
        }

        /* 状态标签样式 */
        .status-badge {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 500;
        }

        .status-predicted {
            background-color: rgba(248, 150, 30, 0.1);
            color: var(--warning-color);
        }

        .status-occurred {
            background-color: rgba(247, 37, 133, 0.1);
            color: var(--error-color);
        }

        .status-resolved {
            background-color: rgba(76, 201, 240, 0.1);
            color: var(--success-color);
        }

        /* 严重性标签 */
        .severity {
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .severity-high {
            color: var(--error-color);
        }

        .severity-medium {
            color: var(--warning-color);
        }

        .severity-low {
            color: var(--success-color);
        }

        /* 底部区域 */
        .card-footer {
            padding: 15px 25px;
            border-top: 1px solid var(--mid-gray);
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 0.9rem;
            color: var(--dark-gray);
        }

        .pagination {
            display: flex;
            gap: 8px;
        }

        .page-btn {
            width: 36px;
            height: 36px;
            border-radius: 6px;
            display: flex;
            align-items: center;
            justify-content: center;
            border: 1px solid var(--mid-gray);
            background-color: white;
            color: var(--text-color);
            cursor: pointer;
            transition: all 0.2s ease;
        }

        .page-btn:hover, .page-btn.active {
            background-color: var(--primary-color);
            color: white;
            border-color: var(--primary-color);
        }

        .back-link {
            color: var(--primary-color);
            text-decoration: none;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 6px;
            transition: color 0.3s ease;
        }

        .back-link:hover {
            color: var(--secondary-color);
            text-decoration: underline;
        }

        /* 响应式调整 */
        @media (max-width: 768px) {
            .navbar {
                flex-direction: column;
                gap: 15px;
            }

            .navbar-actions {
                width: 100%;
                justify-content: flex-end;
            }

            .stats-grid {
                grid-template-columns: 1fr 1fr;
            }

            .card-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }

            .card-actions {
                width: 100%;
                justify-content: flex-end;
            }

            .card-footer {
                flex-direction: column;
                gap: 15px;
                align-items: flex-start;
            }

            .pagination {
                width: 100%;
                justify-content: center;
            }
        }

        @media (max-width: 576px) {
            .stats-grid {
                grid-template-columns: 1fr;
            }

            .navbar-title {
                font-size: 1.2rem;
            }

            .btn {
                padding: 6px 12px;
                font-size: 0.9rem;
            }

            .btn span {
                display: none;
            }

            .btn i {
                margin-right: 0;
            }
        }
    </style>
</head>
<body>
<div class="page-container">
    <!-- 顶部导航栏 -->
    <nav class="navbar">
        <div class="navbar-title">
            <i class="fas fa-shield-alt"></i>
            <span>社区应急服务系统</span>
        </div>
        <div class="navbar-actions">
            <button class="btn btn-primary">
                <i class="fas fa-sync-alt"></i>
                <span>刷新数据</span>
            </button>
        </div>
    </nav>

    <!-- 统计卡片 -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-title">总记录数</div>
            <div class="stat-value">
                <%
                    // 统计总记录数
                    Connection countConn = null;
                    PreparedStatement countStmt = null;
                    ResultSet countRs = null;
                    int totalCount = 0;

                    try {
                        countConn = DatabaseUtil.getConnection();
                        countStmt = countConn.prepareStatement("SELECT COUNT(*) AS total FROM disasters");
                        countRs = countStmt.executeQuery();
                        if (countRs.next()) {
                            totalCount = countRs.getInt("total");
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    } finally {
                        if (countRs != null) countRs.close();
                        if (countStmt != null) countStmt.close();
                        if (countConn != null) countConn.close();
                    }
                    out.print(totalCount);
                %>
            </div>
            <div class="stat-trend trend-up">
                <i class="fas fa-arrow-up"></i> 较上月增长 12%
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-title">已解决灾情</div>
            <div class="stat-value">
                <%
                    // 统计已解决灾情数
                    int resolvedCount = 0;
                    try {
                        countConn = DatabaseUtil.getConnection();
                        countStmt = countConn.prepareStatement("SELECT COUNT(*) AS resolved FROM disasters WHERE status = '已解决'");
                        countRs = countStmt.executeQuery();
                        if (countRs.next()) {
                            resolvedCount = countRs.getInt("resolved");
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    } finally {
                        if (countRs != null) countRs.close();
                        if (countStmt != null) countStmt.close();
                        if (countConn != null) countConn.close();
                    }
                    out.print(resolvedCount);
                %>
            </div>
            <div class="stat-trend trend-up">
                <i class="fas fa-arrow-up"></i> 解决率 78%
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-title">高严重性灾情</div>
            <div class="stat-value">
                <%
                    // 统计高严重性灾情数
                    int highSeverityCount = 0;
                    try {
                        countConn = DatabaseUtil.getConnection();
                        countStmt = countConn.prepareStatement("SELECT COUNT(*) AS high FROM disasters WHERE severity = '高'");
                        countRs = countStmt.executeQuery();
                        if (countRs.next()) {
                            highSeverityCount = countRs.getInt("high");
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    } finally {
                        if (countRs != null) countRs.close();
                        if (countStmt != null) countStmt.close();
                        if (countConn != null) countConn.close();
                    }
                    out.print(highSeverityCount);
                %>
            </div>
            <div class="stat-trend trend-down">
                <i class="fas fa-arrow-down"></i> 较上月下降 5%
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-title">本月新增</div>
            <div class="stat-value">
                <%
                    // 统计本月新增灾情数
                    int monthlyCount = 0;
                    try {
                        countConn = DatabaseUtil.getConnection();
                        countStmt = countConn.prepareStatement(
                                "SELECT COUNT(*) AS monthly FROM disasters WHERE timestamp >= DATE_FORMAT(CURDATE(), '%Y-%m-01')");
                        countRs = countStmt.executeQuery();
                        if (countRs.next()) {
                            monthlyCount = countRs.getInt("monthly");
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    } finally {
                        if (countRs != null) countRs.close();
                        if (countStmt != null) countStmt.close();
                        if (countConn != null) countConn.close();
                    }
                    out.print(monthlyCount);
                %>
            </div>
            <div class="stat-trend trend-up">
                <i class="fas fa-arrow-up"></i> 较上月增长 3%
            </div>
        </div>
    </div>

    <!-- 主内容卡片 -->
    <div class="content-card">
        <div class="card-header">
            <h2 class="card-title">历史灾情信息</h2>
            <div class="card-actions">
                <button class="btn btn-primary">
                    <i class="fas fa-filter"></i>
                    <span>筛选</span>
                </button>
                <button class="btn btn-primary">
                    <i class="fas fa-download"></i>
                    <span>导出</span>
                </button>
            </div>
        </div>

        <div class="table-wrapper">
            <table class="data-table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>名称</th>
                    <th>发生时间</th>
                    <th>地点</th>
                    <th>影响区域</th>
                    <th>状态</th>
                    <th>严重性</th>
                    <th>联系人</th>
                </tr>
                </thead>
                <tbody>
                <%
                    Connection conn = null;
                    PreparedStatement stmt = null;
                    ResultSet rs = null;

                    try {
                        conn = DatabaseUtil.getConnection();
                        String sql = "SELECT * FROM disasters ORDER BY timestamp DESC";
                        stmt = conn.prepareStatement(sql);
                        rs = stmt.executeQuery();

                        boolean hasData = false;
                        while (rs.next()) {
                            hasData = true;
                            String status = rs.getString("status");
                            String severity = rs.getString("severity");
                %>
                <tr>
                    <td><%= rs.getInt("id") %></td>
                    <td>
                        <div style="font-weight: 500;"><%= rs.getString("name") %></div>
                        <div style="font-size: 0.85rem; color: var(--dark-gray);">
                            <%= rs.getString("description").length() > 30
                                    ? rs.getString("description").substring(0, 30) + "..."
                                    : rs.getString("description") %>
                        </div>
                    </td>
                    <td><%= rs.getTimestamp("timestamp") %></td>
                    <td><%= rs.getString("location") %></td>
                    <td><%= rs.getString("affected_area") %></td>
                    <td>
                                <span class="status-badge
                                    <%= status.equals("预测") ? "status-predicted" :
                                       status.equals("已发生") ? "status-occurred" : "status-resolved" %>">
                                    <%= status %>
                                </span>
                    </td>
                    <td>
                                <span class="severity
                                    <%= severity.equals("高") ? "severity-high" :
                                       severity.equals("中") ? "severity-medium" : "severity-low" %>">
                                    <i class="fas fa-exclamation-circle"></i>
                                    <%= severity %>
                                </span>
                    </td>
                    <td><%= rs.getString("contact") %></td>
                </tr>
                <%
                    }

                    // 没有数据时显示空状态
                    if (!hasData) {
                %>
                <tr>
                    <td colspan="8" style="text-align: center; padding: 50px; color: var(--dark-gray);">
                        <div style="font-size: 3rem; margin-bottom: 15px; color: var(--mid-gray);">
                            <i class="fas fa-folder-open"></i>
                        </div>
                        <div style="font-size: 1.1rem; font-weight: 500;">暂无灾情记录</div>
                    </td>
                </tr>
                <%
                    }
                } catch (SQLException e) {
                %>
                <tr>
                    <td colspan="8" style="text-align: center; padding: 30px; color: var(--error-color);">
                        <i class="fas fa-exclamation-triangle"></i>
                        数据库查询出错：<%= e.getMessage() %>
                    </td>
                </tr>
                <%
                    } finally {
                        if (rs != null) try { rs.close(); } catch (SQLException e) { e.printStackTrace(); }
                        if (stmt != null) try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
                        if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); }
                    }
                %>
                </tbody>
            </table>
        </div>

        <div class="card-footer">
            <div>显示 1 - <%= Math.min(totalCount, 10) %> 条，共 <%= totalCount %> 条记录</div>
            <div class="pagination">
                <button class="page-btn"><i class="fas fa-angle-left"></i></button>
                <button class="page-btn active">1</button>
                <button class="page-btn">2</button>
                <button class="page-btn">3</button>
                <button class="page-btn">...</button>
                <button class="page-btn">最后一页</button>
                <button class="page-btn"><i class="fas fa-angle-right"></i></button>
            </div>
        </div>
    </div>

    <!-- 底部返回链接 -->
    <div style="margin-top: 25px; text-align: center;">
        <a href="clviLianPage.jsp" class="back-link">
            <i class="fas fa-arrow-left"></i>
            返回居民首页
        </a>
    </div>
</div>
</body>
</html>